package org.cmroot.certmaster.dao.impl;

import org.cmroot.certmaster.dao.GenericDao;
import org.cmroot.certmaster.dao.GenericDaoException;
import org.cmroot.certmaster.model.X509;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import java.lang.reflect.*;
import java.util.Collection;
import java.util.List;

/**
 * Copyright (c) 2010 Andrey Domas <andrey.domas at gmail.com>.
 * *
 * This file is part of certmaster.
 * *
 * Certmaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * *
 * Certmaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 */

public abstract class GenericDaoHibernateImpl<T> implements GenericDao<T> {

    @Autowired
    private SessionFactory sessionFactory;

    private Session session;

    private final Class<T> persistentClass;

    @SuppressWarnings("unchecked")
    public GenericDaoHibernateImpl() {

        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
    }

    protected Session getSession() {
        if (this.session == null) {
            this.session = sessionFactory.openSession();
        }
        return this.session;
    }

    public Class<T> getPersistentClass() {
        return this.persistentClass;
    }

    @Override
    public void save(T o) throws GenericDaoException {
        try {
            this.getSession().saveOrUpdate(o);
        } catch (DataAccessException e) {
            throw new GenericDaoException(e);
        }
    }

    @Override
    public void saveAll(Collection<T> os) throws GenericDaoException {
        try {
            for (T o : os) {
                this.getSession().saveOrUpdate(o);
            }
        } catch (DataAccessException e) {
            throw new GenericDaoException(e);
        }
    }

    @Override
    public void delete(T o) throws GenericDaoException {
        try {
            this.getSession().delete(o);
        } catch (DataAccessException e) {
            throw new GenericDaoException(e);
        }
    }

    @Override
    public List<T> findAll() throws GenericDaoException {
        try {
            return this.getSession().createCriteria(this.persistentClass).list();
        } catch (DataAccessException e) {
            throw new GenericDaoException(e);
        }
    }

    //TODO - does not work!
    @Override
    public void delete(Long id) throws GenericDaoException {

        Object obj;

        try {
            obj = this.getSession().load(this.getPersistentClass(), id);
        } catch (DataAccessException e) {
            throw new GenericDaoException(e);
        }

        if (obj != null) {
            delete((this.getPersistentClass()).cast(obj));
        }
    }

    @Override
    public T findById(Long id) throws GenericDaoException {
        try {
            return (T) this.getSession().load(getPersistentClass(), id);
        } catch (DataAccessException e) {
            throw new GenericDaoException(e);
        }
    }
}
